<?php
include "../lib/DAL/ChuongTrinhDaoTao.php";
class ChuongTrinhDaoTaoBLL{
    function __construct(){
        
    }
    
    function __destruct(){
        
    }
    public function setCTDT($row){
        $allCTDT=new ChuongTrinhDaoTao();
        $allCTDT->setCTDT($row['ma_ctdt'],$row['ten_td'],$row['ten_hedt'],$row['ten_nganh']);
        return $allCTDT;
    }
    public function set_tbl_ctdt($row){
        $ctdt= new ChuongTrinhDaoTao();
        $ctdt->set_tbl_ctdt($row['ma_ctdt'],$row['madm_td'],$row['madm_hdt'],$row['madm_nganh']);
        return $ctdt;
    }
    public function getAllCTDT(){
        $sql="SELECT C.ma_ctdt,T.ten_td,H.ten_hedt,N.ten_nganh
              FROM tbl_ctdt as C 
              INNER JOIN dm_trinhdo as T ON C.madm_td=T.madm_td
              INNER JOIN dm_hedaotao as H ON C.madm_hdt=H.madm_hdt
              INNER JOIN dm_nganh as N ON C.madm_nganh=N.madm_nganh
              ORDER BY C.ma_ctdt DESC";
        $result = mysql_query($sql) or die(mysql_error());
        $arr_ctdt = array();
        while($row = mysql_fetch_array($result)){
            $arr_ctdt[] = $this->setCTDT($row);
        }
        return $arr_ctdt;
    }
    public function themCTDT($ctdt){
        $sql =" INSERT INTO tbl_ctdt(ma_ctdt, madm_td, madm_hdt, madm_nganh)
                VALUES ('".$ctdt->getma_ctdt()."'
                        ,'".$ctdt->getmadm_td()."'
                        ,'".$ctdt->getmadm_hdt()."'
                        ,'".$ctdt->getmadm_nganh()."'
                )";
        echo $sql;
        $result = mysql_query($sql) or die (mysql_error());
        print_r($result);
        return $result;
    }
    public function dulieutheoma($ma){
        $sql="  SELECT ma_ctdt, madm_td, madm_hdt, madm_nganh
                FROM tbl_ctdt
                WHERE ma_ctdt LIKE '".$ma."'";
        $rs = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($rs);
    }
    public function suaCTDT($ctdt){
        $sql="  UPDATE tbl_ctdt
                SET  madm_td        = '".$ctdt->getmadm_td()."' 
                    ,madm_hdt       = '".$ctdt->getmadm_hdt()."'
                    ,madm_nganh     = '".$ctdt->getmadm_nganh()."'
                WHERE ma_ctdt ='".$ctdt->getma_ctdt()."'";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    public function xoaCTDT($maxoa){
        $sql="DELETE FROM tbl_ctdt WHERE ma_ctdt = '".$maxoa."'";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    public function xoaChonCTDTAll(){
        $sql="DELETE FROM tbl_ctdt";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    function buildSearchQueryLike($keyword){    
		$dieukien="";   
		$qse=" OR ";
		$arr = tachChuoi($keyword);	
		foreach($arr as $kw){						
			$dieukien .=" T.ten_td LIKE '%$kw%' OR H.ten_hedt LIKE '%$kw%' OR N.ten_nganh LIKE '%$kw%'";			
			$dieukien .= $qse;
							
		}
		$cstr = strlen($qse);
		$dieukien = substr($dieukien,0,-$cstr);		
		return $dieukien;	
		
	}
    function timkiem($where){
        $sql="SELECT C.ma_ctdt,T.ten_td,H.ten_hedt,N.ten_nganh
              FROM tbl_ctdt as C 
              INNER JOIN dm_trinhdo as T ON C.madm_td=T.madm_td
              INNER JOIN dm_hedaotao as H ON C.madm_hdt=H.madm_hdt
              INNER JOIN dm_nganh as N ON C.madm_nganh=N.madm_nganh 
              $where
              ORDER BY C.ma_ctdt DESC";
        $ex=mysql_query($sql);
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $dstk[]=$this->setCTDT($row);
            }
        }
        return $dstk;
        print_r($dstk);
    }
    public function getPages($where,$offset,$limit){
        $result = mysql_query(" SELECT C.ma_ctdt,T.ten_td,H.ten_hedt,N.ten_nganh
                                FROM tbl_ctdt as C 
                                INNER JOIN dm_trinhdo as T ON C.madm_td=T.madm_td
                                INNER JOIN dm_hedaotao as H ON C.madm_hdt=H.madm_hdt
                                INNER JOIN dm_nganh as N ON C.madm_nganh=N.madm_nganh  
                                $where 
                                LIMIT $offset, $limit");
        $arr_ctdt= array();
        
        while($row = mysql_fetch_array($result)){
            $arr_ctdt[] =  $this->setCTDT($row);
        }
        return $arr_ctdt;
    }
    function paging($totalRecords,$limit,$url,$css){
		$totalPage=ceil($totalRecords/$limit);
		$pager='';
		for($i=1;$i<=$totalPage;$i++){
            $pager.="<a href='$url&p=$i' class='$css'>$i</a>";
		}
		return $pager;
	}
}
?>